package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //metaObject当前被填充的对象
        Thread thread = Thread.currentThread();
        log.info("[公共字段自动填充，插入],当前线程id:{}",thread.getId());
        log.info("[公共字段自动填充，插入]，{}",metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
      /*  metaObject.setValue("createUser", new Long(1));
        metaObject.setValue("updateUser", new Long(1));*/
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Thread thread = Thread.currentThread();
        log.info("[公共字段自动填充，更新数据],当前线程id:{}",thread.getId());
        log.info("[公共字段自动填充，更新数据]，{}",metaObject.toString());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        //metaObject.setValue("updateUser", new Long(1));

    }
}
